<?php


if (version_compare(PHP_VERSION, '5.2.0','<'))die('require PHP > 5.2.0 !');

$GLOBALS['_beginTime']=microtime(true);
define('MEMORY_LIMIT_ON',function_exists('memory_get_usage'));
if (MEMORY_LIMIT_ON)$GLOBALS['_startUseMems']=memory_get_usage(); 
header('Content-Type:text/html;charset=utf-8');
date_default_timezone_set('Asia/Shanghai');

//定义项目和框架的目录
//index.php为每个应用的首页即每个应用的入口;$_SERVER['SCRIPT_FILENAME']当前执行脚本绝对路径
defined('APP_BASE') or define('APP_BASE', str_replace('\\','/',dirname($_SERVER['SCRIPT_FILENAME'])).'/');
defined('CR_BASE') or define('CR_BASE', str_replace('\\','/',dirname(__FILE__)).'/');
//项目名称
defined('APP_NAME') or define('APP_NAME', basename(dirname($_SERVER['SCRIPT_FILENAME'])));
//定义框架目录路径,方便后期维护修改
define('CR_COM', CR_BASE.'common/');
define('CR_CORE', CR_BASE.'core/');
define('CR_LIB', CR_BASE.'libraries/');

define('magic_quotes_gpc', get_magic_quotes_gpc());

require CR_COM.'functions.php';


//defined('ROOT_PATH') or define('ROOT_PATH', dirname(CRPHP_PATH).'/');

//defined('APP_NAME') or define('APP_NAME', basename(dirname($_SERVER['SCRIPT_FILENAME'])));
//defined('APP_DEBUG') or define('APP_DEBUG', false); //默认关闭调试
//defined('RUNTIME_PATH') or define('RUNTIME_PATH', APP_PATH.'runtime/');



/*$_cur_file=rtrim($_SERVER['SCRIPT_NAME'],'/');
if (strtoupper(APP_NAME)== strtoupper(basename(dirname($_cur_file))))
    $_root=dirname(dirname($_cur_file));
else {
    $_root=dirname($_cur_file);
}*/
//ROOT_PATH目录就是项目首页(index.php)的父目录,即项目根目录,如前台标签a
//define('ROOT_PATH',($_root=='/' || $_root=='\\')?'':rtrim($_root,'/').'/');

//web的根目录,如果是二级域名则根目录为二级域名映射的目录
//define('DOC_ROOT',rtrim(str_replace($_SERVER['SCRIPT_NAME'],'',$_SERVER['SCRIPT_FILENAME']),'/').'/');

//加载部署类,先建立好目录
require CR_CORE.'deploy.php';
Deploy::build();

//项目路径
define('APP_CONF',APP_BASE.'conf/');
define('APP_EXT',APP_BASE.'extend/');
define('APP_INC',APP_BASE.'includes/');
define('APP_MODULE',APP_BASE.'includes/app/');
define('APP_MODLE',APP_BASE.'includes/model/');
define('APP_TPL',APP_BASE.'themes/');
define('APP_COM',APP_BASE.'common/');
define('APP_DATA',APP_BASE.'data/');





//应用配置文件
$config=APP_CONF.'config.php';
if (file_exists($config)){
    cr_define(array_merge(include CR_COM.'config.php' , include $config));
}else {
    //框架配置文件
    cr_define(include CR_COM.'config.php');
}

if (defined('APP_DEBUG') && APP_DEBUG==true){
    //调试时把错误，统计等的信息统一由Debug类来处理,处理包含保存、显示信息
    $GLOBALS['_debug']=true;
    error_reporting(E_ALL);
    include CR_CORE.'debug.php';
    Debug::start();
    set_error_handler(array('Debug','catcher'));
}else {
    //发布产品时用指定的文本日志保存错误等信息
    ini_set('display_errors', 'Off');
    ini_set('log_errors', 'On');
    ini_set('error_log', APP_DATA.'log/error_log');
}


$funfile=APP_COM.'functions.php';
if (file_exists($funfile))
    include $funfile;

/* 包含文件路径，暂时用不上 */
 $include_path=get_include_path();
 $include_path.=PATH_SEPARATOR.CR_CORE;
  set_include_path($include_path);
  function __autoload($className){
    if ($className=='memcached'){
        
    }else {
        require_once strtolower($className).'.php';
    }
    Debug::addMsg('<b>'.$className.'</b>类',1);
  }



//缓存
if (!CACHE_SERVER){
    Debug::addMsg('<font color="red">没有开启页面缓存！</font>');
}else{
    Debug::addMsg('开启了页面缓存，实现页面静态化');
}

if (CACHE_SERVER=='memcached'){
    if (extension_loaded('memcache')){
        $mem=& cache_server('memcached');
        if (!$mem->status()){
            Debug::addMsg('<font color="red">连接memcache服务器失败，请检查！</font>');
        }else {
            define('USEMEM', true);
            Debug::addMsg('启用了Memcache');
        }
    }else {
        Debug::addMsg('<font color="red">PHP没有安装memcache扩展模块，请先安装！</font>');
    }
}else {
    Debug::addMsg('<font color="red">没有使用memcache</font>');
}

if (defined('USEMEM')){
    import('cr.libraries.memsession');
    MemSession::start($mem->getMc());
    Debug::addMsg('开始会话Session(使用memcache保存会话信息)');
}else {
    session_start();
    Debug::addMsg('<font color="red">开启会话Session</font>(但没使用memcache,开启后自动使用memcache)');
}
Debug::addMsg('会话ID:'.session_id());


//url解析
Url::parse();










class Object{
    protected $_errornum=0;
    protected $_errors=array();
    
    public function __construct($params=array()){
        $this->Object($params);
    }
    
    public function Object($params=array()){
        $this->_initialize($params);
    }
    
    public function _error($msg,$obj=''){
        if (is_array($msg)){
            $this->_errors=array_merge($this->_errors,$msg);
            $this->_errornum+=count($msg);
        }else {
            $this->_errors[]=compact('msg','obj');
            $this->_errornum++;
        }
    }
    
    public function get_error(){
        return $this->_errors;
    }
    
    public function has_error(){
        return $this->_errornum;
    }
}